﻿var option = {
    treeId: "",
    xmlpath: ""
}

function gridtree_ec(id)
{

    //debugger;
    if ($("#gridtree_container").children().length == 0)
    {

        return;
    }
    var jQNode = null;
    if (id)
    {

        jQNode = $($.id(id));
    } else
    {

        jQNode = $("#gridtree_container").children().eq(0)
    }

    var srcElement = null;
    if (arguments.length == 1)
    {

        srcElement = event.srcElement;
    } else
    {

        srcElement = jQNode.find("u")[0];
    }
    var child_nodes = jQNode.children("div");
    var status = srcElement.className;
    if (child_nodes.length > 0)
    {  //说明已经加载了子节点
        if (status == "collapse")
        {


            child_nodes.show();
            srcElement.className = "expand";
        }
        else
        {

            child_nodes.hide();
            srcElement.className = "collapse";
        }
    } else
    {


        srcElement.className = "loading";
        $.ajax({
            url: "/CustomControl/GridTreeDelay/Pub_XMLHTTP.aspx?ywtype=GridTreeRequest",
            //cache: false,
            //最好用异步,因为同步的ajax请求会锁定浏览器进程,如果请求太慢会造成浏览器假死的现象
            async: false,
            type: "POST",
            data: {
                pid: id ? $.toUTF8(id) : $.toUTF8(jQNode.attr("id")),
                treeId: option.treeId,
                xmlpath: option.xmlpath,
                sql: $.toUTF8($("#sqlstring").html())
            },
            success: function(res)
            {
                if (res == "")
                {
                    srcElement.className = "noimg";
                } 
                else
                {

                    srcElement.className = "expand";
                    //jQNode.append(res);
                    jQNode[0].innerHTML = jQNode.html() + res
                    child_nodes = jQNode.children("div");
                    jQNode.find("table").unbind("click").click(function()
                    {

                        $(".gridtree_selected").removeClass("gridtree_selected");
                        $(this).addClass("gridtree_selected");
                    })
                    child_nodes.find("u").css("marginLeft", ($.toInt(srcElement.style.marginLeft) + 20) + "px");
                    child_nodes.show();
                    jQNode.find("div").each(function(i, n)
                    {

                        var node = $(n).find(".gridText").eq(0)
                        $(node).attr("style", "position:absolute; padding-top:4px;text-overflow:ellipsis;white-space:nowrap;overflow:hidden").width($(node).parent().width() - $(node).position().left + 3)
                    })
                }
            }
        });
    }
}

function initGridTree()
{
    var node = $("#gridtree_container").find(".gridText").each(function(i, node)
    {
        if (i != 0)
        {
            $(node).width($(node).parent().width() - $(node).position().left + 3)
        }
    })
}

function selectRow(grid, id)
{
    $(".gridtree_selected").removeClass("gridtree_selected");
    var selectNode = null;
    if (id)
    {

        selectNode = $($.id(id)).find("table").eq(0);
    } else
    {

        selectNode = $("#gridtree_container").find("table").eq(0);
    }
    selectNode.addClass("gridtree_selected");
    grid.selectedRow = selectNode[0]
}

function refreshNode(grid, id)
{

    var pNode = $($.id(id));
    pNode.children("div").remove();
    gridtree_ec(id, true);
    selectRow(grid, id);
}

